﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LIMS.SqlServerDAL
{
    public class LabOrderService : LIMS.IDAL.ILabOrder
    {
        LIMSDataContext DB = new LIMSDataContext();

        /// <summary>
        /// 查询指定的实验室能否被预约。返回false表示该实验室在指定条件下可以被预约；否则返回true
        /// </summary>
        /// <param name="orderTestTime">指定预约日期</param>
        /// <param name="labID">指定预约的实验室ID</param>
        /// <param name="orderClassTimeCode">指定预约上课时间段Code</param>
        /// <returns>返回false表示该实验室在指定条件下可以被预约；否则返回true</returns>
        public Boolean IsCanOrder(DateTime orderTestTime, String labID, LIMS.Model.OrderClassTimeSpan orderClassTimeSpan)
        {
            //查找实验日期，如果数据库没有该日期的预约数据，说明在该时间下可以被预约
            var labTmp1 = DB.TB_lab.Where(s => s.labStatusDate == orderTestTime).Select(s => s);
            var labTmp2 = DB.TB_lab.Where(sg => sg.labStatusDate == orderTestTime && sg.labID == labID).Select(s => s);
            if (labTmp1.Count() == 0 || labTmp2.Count() == 0)
            {
                return false;
            }
            return ConvertToLabStatus(orderClassTimeSpan, labTmp2.First());
        }

        /// <summary>
        /// 将上课时间段（orderClassTimeSpan）和指定的实验室实体类（lab），返回该实体类对应的labStatus的bool值
        /// </summary>
        /// <param name="orderClassTimeSpan">指定的上课时间段</param>
        /// <param name="lab">指定的实验室实体类（lab）</param>
        /// <returns>该实体类对应的labStatus的bool值</returns>
        private static bool ConvertToLabStatus(Model.OrderClassTimeSpan orderClassTimeSpan, TB_lab lab)
        {
            Boolean blResult = false;
            switch (orderClassTimeSpan)
            {
                case LIMS.Model.OrderClassTimeSpan.None:
                    break;
                case LIMS.Model.OrderClassTimeSpan.labStatus12:
                    blResult = lab.labStatus12;
                    break;
                case LIMS.Model.OrderClassTimeSpan.labStatus34:
                    blResult = lab.labStatus34;
                    break;
                case LIMS.Model.OrderClassTimeSpan.labStatus56:
                    blResult = lab.labStatus56;
                    break;
                case LIMS.Model.OrderClassTimeSpan.labStatus78:
                    blResult = lab.labStatus78;
                    break;
                case LIMS.Model.OrderClassTimeSpan.labStatus910:
                    blResult = lab.labStatus910;
                    break;
                default:
                    break;
            }
            return blResult;
        }

        /// <summary>
        /// 插入一条预约数据
        /// </summary>
        /// <param name="order">待写入数据库的预约实体对象</param>
        /// <returns></returns>
        public Boolean InsertOrderInfo(LIMS.Model.OrderLabs order)
        {
            #region TODO:(1）根据OrderLabs表两个参数，指定的日期和实验室ID，查询在这条记录在Lab表中存不存在？
            var tmpCount = DB.TB_lab.Where(s => s.labStatusDate == order.orderTestDate && s.labID == order.labID).Count();
            #endregion

            #region TODO:(2) 若存在就将TB_lab指定的上课时间段的值设置为true；否则就在TB_lab新添加一条记录，并将指定的上课时间段的值设置为true
            if (tmpCount > 0)
            {
                var tb_lab = DB.TB_lab.Single(w => w.labStatusDate == order.orderTestDate && w.labID == order.labID);
                #region  根据参数order.orderClassTime，将TB_lab指定的上课时间段（labStatus）的值设置为true
                switch (order.orderClassTimeSpan)
                {
                    case LIMS.Model.OrderClassTimeSpan.None:
                        break;
                    case LIMS.Model.OrderClassTimeSpan.labStatus12:
                        tb_lab.labStatus12 = true;
                        break;
                    case LIMS.Model.OrderClassTimeSpan.labStatus34:
                        tb_lab.labStatus12 = true;
                        break;
                    case LIMS.Model.OrderClassTimeSpan.labStatus56:
                        tb_lab.labStatus12 = true;
                        break;
                    case LIMS.Model.OrderClassTimeSpan.labStatus78:
                        tb_lab.labStatus12 = true;
                        break;
                    case LIMS.Model.OrderClassTimeSpan.labStatus910:
                        tb_lab.labStatus12 = true;
                        break;
                    default:
                        break;
                }
                //switch (order.orderClassTimeSpan)
                //{

                //    case "labStatus12":
                //        tb_lab.labStatus12 = true;
                //        break;
                //    case "labStatus34":
                //        tb_lab.labStatus34 = true;
                //        break;
                //    case "labStatus56":
                //        tb_lab.labStatus56 = true;
                //        break;
                //    case "labStatus78":
                //        tb_lab.labStatus78 = true;
                //        break;
                //    case "labStatus910":
                //        tb_lab.labStatus910 = true;
                //        break;
                //    default:
                //        break;
                //}
                #endregion
            }
            else
            {
                #region 在TB_lab新添加一条指定的记录，并根据参数order.orderClassTime，将该记录指定的（labStatus）值设置为true
                TB_lab labEntity = new TB_lab() { labID = order.labID, labStatusDate = order.orderTestDate };
                switch (order.orderClassTimeSpan)
                {
                    case LIMS.Model.OrderClassTimeSpan.None:
                        break;
                    case LIMS.Model.OrderClassTimeSpan.labStatus12:
                        labEntity.labStatus12 = true;
                        break;
                    case LIMS.Model.OrderClassTimeSpan.labStatus34:
                        labEntity.labStatus12 = true;
                        break;
                    case LIMS.Model.OrderClassTimeSpan.labStatus56:
                        labEntity.labStatus12 = true;
                        break;
                    case LIMS.Model.OrderClassTimeSpan.labStatus78:
                        labEntity.labStatus12 = true;
                        break;
                    case LIMS.Model.OrderClassTimeSpan.labStatus910:
                        labEntity.labStatus12 = true;
                        break;
                    default:
                        break;
                }
                #endregion
                DB.TB_lab.InsertOnSubmit(labEntity);
            }
            #endregion

            #region TODO:(3) 将实例对象order的相关数据写入TB_orderLab表
            TB_orderLab orderLabEntity = new TB_orderLab()
            {
                labId = order.labID,
                orderClassTimeSpan = order.orderClassTimeSpan.ToString(),
                orderTestDate = order.orderTestDate,
                orderOperationTime = order.orderOperationTime,
                testDetail = order.testDetail,
                teacherId = order.teacherID
            };
            DB.TB_orderLab.InsertOnSubmit(orderLabEntity);
            #endregion

            DB.SubmitChanges();
            return true;
        }

        /// <summary>
        /// 获取预约总项数
        /// </summary>
        /// <returns></returns>
        public Int32 TotalOrders()
        {
            return DB.TB_orderLab.Select(s => s).Count();
        }

        /// <summary>
        /// 未完成，删除指定的预约数据
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public Boolean RemoveOrder(Int32 orderID)
        {
            return true;
        }

        /// <summary> 
        /// 未完成，更新指定的预约信息
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public Boolean Modify(LIMS.Model.OrderLabs order)
        {
            return true;
        }

        #region 多种查询方式

        /// <summary>
        /// 获取所有的实验室预约信息
        /// </summary>
        /// <returns></returns>
        public List<LIMS.Model.OrderLabs> FindAll()
        {
            List<LIMS.Model.OrderLabs> listTmp = new List<LIMS.Model.OrderLabs>();
            DB.TB_orderLab.Select(s => s).ToList().ForEach((item) =>
            {
                LIMS.Model.OrderLabs orderLabTmp = new LIMS.Model.OrderLabs()
                {
                    ID = item.ID,
                    labID = item.labId,
                    orderClassTimeSpan = (Model.OrderClassTimeSpan)Enum.Parse(typeof(Model.OrderClassTimeSpan), item.orderClassTimeSpan),
                    orderOperationTime = item.orderOperationTime,
                    orderTestDate = item.orderTestDate,
                    teacherID = item.teacherId,
                    testDetail = item.testDetail
                };
                listTmp.Add(orderLabTmp);
            });
            return listTmp;
        }

        /// <summary>
        /// 根据预约说明来获取所有与之相关的预约数据
        /// </summary>
        /// <param name="strDetail">预约说明</param>
        /// <returns></returns>
        public List<LIMS.Model.OrderLabs> FindByDetail(String strDetail)
        {
            List<LIMS.Model.OrderLabs> listTmp = new List<LIMS.Model.OrderLabs>();
            DB.TB_orderLab.Where(w => w.testDetail.Contains(strDetail)).ToList().Select(s => s).ToList().ForEach(
                (item) =>
                {
                    LIMS.Model.OrderLabs orderLabTmp = new LIMS.Model.OrderLabs()
                    {
                        ID = item.ID,
                        labID = item.labId,
                        orderClassTimeSpan = (Model.OrderClassTimeSpan)Enum.Parse(typeof(Model.OrderClassTimeSpan), item.orderClassTimeSpan),
                        orderOperationTime = item.orderOperationTime,
                        orderTestDate = item.orderTestDate,
                        teacherID = item.teacherId,
                        testDetail = item.testDetail
                    };
                    listTmp.Add(orderLabTmp);
                });
            return listTmp;
        }

        /// <summary>
        /// 根据教师ID获取与之相关的实验项目
        /// </summary>
        /// <param name="teacherID"></param>
        /// <returns></returns>
        public List<LIMS.Model.OrderLabs> FindByTeacherID(String teacherID)
        {
            List<LIMS.Model.OrderLabs> listTmp = new List<LIMS.Model.OrderLabs>();
            DB.TB_orderLab.Where(w => w.teacherId == teacherID).ToList().Select(s => s).ToList().ForEach(
                (item) =>
                {
                    LIMS.Model.OrderLabs orderLabTmp = new LIMS.Model.OrderLabs()
                    {
                        ID = item.ID,
                        labID = item.labId,
                        orderClassTimeSpan = (Model.OrderClassTimeSpan)Enum.Parse(typeof(Model.OrderClassTimeSpan), item.orderClassTimeSpan),
                        orderOperationTime = item.orderOperationTime,
                        orderTestDate = item.orderTestDate,
                        teacherID = item.teacherId,
                        testDetail = item.testDetail
                    };
                    listTmp.Add(orderLabTmp);
                });
            return listTmp;
        }

        /// <summary>
        /// 根据指定的预约ID获取预约数据
        /// </summary>
        /// <param name="labID"></param>
        /// <returns></returns>
        public List<LIMS.Model.OrderLabs> FindByLabID(String labID)
        {
            List<LIMS.Model.OrderLabs> listTmp = new List<LIMS.Model.OrderLabs>();
            DB.TB_orderLab.Where(w => w.labId == labID).ToList().Select(s => s).ToList().ForEach(
                (item) =>
                {
                    LIMS.Model.OrderLabs orderLabTmp = new LIMS.Model.OrderLabs()
                    {
                        ID = item.ID,
                        labID = item.labId,
                        orderClassTimeSpan = (Model.OrderClassTimeSpan)Enum.Parse(typeof(Model.OrderClassTimeSpan), item.orderClassTimeSpan),
                        orderOperationTime = item.orderOperationTime,
                        orderTestDate = item.orderTestDate,
                        teacherID = item.teacherId,
                        testDetail = item.testDetail
                    };
                    listTmp.Add(orderLabTmp);
                });
            return listTmp;
        }

        /// <summary>
        /// 根据预约日期来获取该日期下所有数据
        /// </summary>
        /// <param name="orderTestTime">指定的预约日期</param>
        /// <returns></returns>
        public List<LIMS.Model.OrderLabs> FindByTestTime(DateTime orderTestTime)
        {
            List<LIMS.Model.OrderLabs> listTmp = new List<LIMS.Model.OrderLabs>();
            DB.TB_orderLab.Where(w => w.orderTestDate == orderTestTime).ToList().Select(s => s).ToList().ForEach(
                (item) =>
                {
                    LIMS.Model.OrderLabs orderLabTmp = new LIMS.Model.OrderLabs()
                    {
                        ID = item.ID,
                        labID = item.labId,
                        orderClassTimeSpan = (Model.OrderClassTimeSpan)Enum.Parse(typeof(Model.OrderClassTimeSpan), item.orderClassTimeSpan),
                        orderOperationTime = item.orderOperationTime,
                        orderTestDate = item.orderTestDate,
                        teacherID = item.teacherId,
                        testDetail = item.testDetail
                    };
                    listTmp.Add(orderLabTmp);
                });
            return listTmp;
        }

        /// <summary>
        /// 根据上课时间段来获取后续日期所有这个时间段的预约数据
        /// </summary>
        /// <param name="orderClassTime">指定的上课时间段</param>
        /// <returns></returns>
        public List<LIMS.Model.OrderLabs> FindByClassTime(LIMS.Model.OrderClassTimeSpan orderClassTimeSpan)
        {
            List<LIMS.Model.OrderLabs> listTmp = new List<LIMS.Model.OrderLabs>();
            DB.TB_orderLab.Where(w => w.orderClassTimeSpan == orderClassTimeSpan.ToString()).ToList().Select(s => s).ToList().ForEach(
                (item) =>
                {
                    LIMS.Model.OrderLabs orderLabTmp = new LIMS.Model.OrderLabs()
                    {
                        ID = item.ID,
                        labID = item.labId,
                        orderClassTimeSpan = (Model.OrderClassTimeSpan)Enum.Parse(typeof(Model.OrderClassTimeSpan),item.orderClassTimeSpan),
                        orderOperationTime = item.orderOperationTime,
                        orderTestDate = item.orderTestDate,
                        teacherID = item.teacherId,
                        testDetail = item.testDetail
                    };
                    listTmp.Add(orderLabTmp);
                });
            return listTmp;
        }

        #endregion



    }
}
